﻿using System;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Shapes;
using PairingMatrix.Common.Enum;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class SetSecondaryCardPositionsOfProjectCardBehavior : Behavior<ProjectCardControl>
    {
        private ContentPresenter _parent;
        private ProjectCardViewModel _projectCardViewModel;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
                                       {
                                           _projectCardViewModel = (ProjectCardViewModel) AssociatedObject.DataContext;
                                           _projectCardViewModel.PropertyChanged += ProjectCard_PropertyChanged;
                                           _projectCardViewModel.SecondaryProjectCards.CollectionChanged += SecondaryProjectCards_CollectionChanged;
                                       };

        }


        private void SecondaryProjectCards_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ResetAllSecondaryProjectCardPositions();
        }

        private void ResetAllSecondaryProjectCardPositions()
        {
            for (int i = 0; i < _projectCardViewModel.SecondaryProjectCards.Count; i++)
            {
                var secondaryCard = _projectCardViewModel.SecondaryProjectCards[i];
                secondaryCard.PositionOnWallOnly = new Point(_projectCardViewModel.Position.X, _projectCardViewModel.Position.Y + 60 * (i + 1));
            }
        }

        void ProjectCard_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Position" || e.PropertyName == "IsCardVisibleInCurrentWall")
            {
                ResetAllSecondaryProjectCardPositions();
            }

        }
    }
}
